package com.zhuge_new.Adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.zhuge_new.R;
import com.zhuge_new.bean.ShareBean;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.BitmapUtils;
import com.zhuge_new.untils.GlideUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/11/2.
 */

public class ShareBottonAdapter extends BaseQuickAdapter<ShareBean.ObjectBean.DataListBean,BaseViewHolder> {


    private Context context;
    private List<ShareBean.ObjectBean.DataListBean> data;
    private RecyclerView recyclerView;

    public ShareBottonAdapter(Context context, @Nullable ShareBean.ObjectBean data, RecyclerView recyclerView) {
        super(R.layout.item_share,data.getDataList());
        this.context=context;
        this.data=data.getDataList();
        this.recyclerView=recyclerView;
    }



    @SuppressLint("ResourceAsColor")
    @Override
    protected void convert(BaseViewHolder helper, ShareBean.ObjectBean.DataListBean item) {
        ImageView iv_share=helper.getView(R.id.iv_share);
        ImageView iv_logo=helper.getView(R.id.iv_share_logo);
        String share=BaseHttp.baseImg1+item.getBigPicture();
        String logo=item.getShareUrl();
        int whith=700;
        int hight=400;
        GlideUtils.loadClippingImageSize(context,share,whith,hight,iv_share);
        Bitmap bitmap=BitmapUtils.localityBitmap(context,R.mipmap.ic_launcher);
        iv_logo.setImageBitmap(CodeUtils.createImage(logo,50,50,bitmap));
        RelativeLayout share_backround=helper.getView(R.id.share_backround);
        share_backround.getBackground().setAlpha(254);
        if (item.getIsFlag()){
            share_backround.setBackgroundColor(R.color.colorGray);
        }else {
            share_backround.setBackgroundColor(R.color.red);
        }
    }

    /**
     * 获取被选中的位置，移到中间，刷新
     * @param     selectedPosition

     */
    private int selectedPosition;

    public void getSelectedPosition(int selectedPosition) {
        this.selectedPosition=selectedPosition;
        moveToMiddle(selectedPosition);
        notifyDataSetChanged();
    }

    private void moveToMiddle(int position) {
        int firstItem = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
        int lastItem=((LinearLayoutManager)recyclerView.getLayoutManager()).findLastVisibleItemPosition();
        //中间位置
        int middle=(firstItem+lastItem)/2;
        // 取绝对值，index下标是当前的位置和中间位置的差，下标为index的view的top就是需要滑动的距离
        int index=(position - middle) >=0 ? position - middle : -(position-middle);
        if (index >=recyclerView.getChildCount()){
            recyclerView.scrollToPosition(position);
        }else {
            if (position < middle){
                recyclerView.scrollBy(0, -recyclerView.getChildAt(index).getTop());
            }else {
                recyclerView.scrollBy(0,recyclerView.getChildAt(index).getTop());
            }
        }
    }
}
